/*
 * Copyright 2006-2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.martinimix.service.account;

import java.util.Comparator;

import net.martinimix.domain.account.Address;

/**
 * Provides an address comparator.
 * 
 * @author Scott Rossillo
 *
 */
class DefaultAddressComparator implements Comparator {

	/* (non-Javadoc)
	 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
	 */
	public final int compare(Object o1, Object o2) {
	
		return compare((Address) o1, (Address) o2);
	}

	/**
	 * Compares the given addresses sorting a default address to the front
	 * of a collection.
	 * 
	 */
	protected int compare(Address a1, Address a2) {
		
		if(a1.isDefaultAddress() || a2.isDefaultAddress()) {
			return a1.isDefaultAddress() ? -1 : 1;
		}
		
		// if neither is default, just pick based on id
		return a1.getId().compareTo(a2.getId()); 
	}
}
